/**
 * Copyright &copy; com.nmf
 */
package com.nmf.site.biz.bill.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.nmf.dao.gen.bean.BizProjectBill;
import com.nmf.site.biz.bill.enums.BillStatusEnums;
import com.nmf.site.biz.bill.service.BizProjectBillService;
import com.nmf.site.common.config.Global;
import com.nmf.site.common.excep.SiteException;
import com.nmf.site.common.persistence.Page;
import com.nmf.site.common.utils.StringUtils;
import com.nmf.site.common.web.BaseController;
import com.nmf.site.modules.sys.entity.User;
import com.nmf.site.modules.sys.utils.UserUtils;

/**
 * 工程单据Controller
 * @author 北冥
 * @version 2019-01-25
 */
@Controller
@RequestMapping(value = "${adminPath}/bill/bizProjectBill/ytt")
public class YttBizProjectBillController extends BaseController {

	@Autowired
	private BizProjectBillService bizProjectBillService;
	
	private String yttFormUrl = "biz/bill/ytt/bizProjectBillForm";
	private String yttListUrl =	 "biz/bill/ytt/bizProjectBillList";
	@ModelAttribute
	public BizProjectBill get(@RequestParam(required=false) String id,@RequestParam(required=false) String opt,HttpServletRequest request) {
		BizProjectBill entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = bizProjectBillService.get(id,opt);
		}
		if (StringUtils.isNotBlank(opt)){
			request.setAttribute("opt", opt);
		}
		if (entity == null){
			entity = new BizProjectBill();
		}
		return entity;
	}
	
	
	/**
	 * 依托单位信息填报
	 * @param bizProjectBill
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = {"list",""})
	public String yttList(BizProjectBill bizProjectBill, HttpServletRequest request, HttpServletResponse response, Model model) {
		try{
			Page<BizProjectBill> page = bizProjectBillService.findYttPage(new Page<BizProjectBill>(request, response), bizProjectBill); 
			model.addAttribute("page", page);
		}catch(SiteException e){
			model.addAttribute("message", e.getMessage());
			return yttListUrl;
		}
		return yttListUrl;
	}
	
	
	
	@RequestMapping(value = "save")
	public String yttSave(BizProjectBill bizProjectBill, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, bizProjectBill)){
			model.addAttribute("bizProjectBill", bizProjectBill);
			return yttFormUrl;
		}
		User user = UserUtils.getUser();
		try{
			bizProjectBillService.save(bizProjectBill,user);
		}catch(SiteException e){
			addMessage(redirectAttributes, e.getMessage());
			return "redirect:"+Global.getAdminPath()+"/bill/bizProjectBill/ytt/?repage";
		}
		addMessage(redirectAttributes, "保存工程单据成功");
		return "redirect:"+Global.getAdminPath()+"/bill/bizProjectBill/ytt/?repage";
	}
	
	@RequestMapping(value = "form")
	public String yttForm(BizProjectBill bizProjectBill, Model model) {
		model.addAttribute("bizProjectBill", bizProjectBill);
		return "biz/bill/ytt/bizProjectBillForm";
	}
	
	@RequiresPermissions("bill:bizProjectBill:edit")
	@RequestMapping(value = "delete")
	public String delete(BizProjectBill bizProjectBill, RedirectAttributes redirectAttributes) {
		try{
			bizProjectBillService.delete(bizProjectBill);
		}catch(SiteException e){
			addMessage(redirectAttributes, e.getMessage());
			return "redirect:"+Global.getAdminPath()+"/bill/bizProjectBill/ytt/?repage";
		}
		addMessage(redirectAttributes, "删除工程单据成功");
		return "redirect:"+Global.getAdminPath()+"/bill/bizProjectBill/ytt/?repage";
	}

}